package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements DeviceKeyMonitor.OnKeyListener {

    DeviceKeyMonitor deviceKeyMonitor;
    ViewGroup group;

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        deviceKeyMonitor = new DeviceKeyMonitor(this, this);
        group = (ViewGroup) this.getWindow().getDecorView();
    }

    @Override
    public void onRecentClick() {
        this.addImageView();
    }

    // 高斯模糊
    public void addImageView() {
//        group = (ViewGroup) this.getWindow().getDecorView();
        group.removeView(imageView);
        imageView = new ImageView(this);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(params);

//      截图，并将图片高斯模糊处理
        Bitmap bitmap = BgBlurUtils.rsBlur(MainActivity.this, BgBlurUtils.activityShot(MainActivity.this), 20);
//      将处理后的图片填充到imageview
        imageView.setImageBitmap(bitmap);
        group.addView(imageView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("TAG", "onResume: ");
        if (group == null || group.getChildCount() <= 0) {
            return;
        }
        for (int i = 0; i < group.getChildCount(); i++) {
            group.removeView(imageView);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
//        this.addImageView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        deviceKeyMonitor.unregister();
    }
}